package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.Result;
import com.itheima.mapper.*;
import com.itheima.pojo.*;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Autowired
    private CheckgroupMapper checkgroupMapper;
    @Autowired
    private Checkgroup_checkitemMapper checkgroup_checkitemMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public Result getAllSetmeal() {
        List<Setmeal> data = setmealMapper.selectList(null);
        for (Setmeal setmeal : data) {
            System.out.println(setmeal+"...................setmeal");
        }
        return new Result(true,"查询全部成功",data);
    }
//根据id查询套餐
    @Override
    public Result findById(Integer id) {
        //先根据套餐id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据套餐id通过套餐项目组查询对应套餐中项目组id，再去项目组查询具体信息
        LambdaQueryWrapper<SetmealCheckgroup> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SetmealCheckgroup::getSetmealId,id);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(wrapper);
        List<CheckGroup>checkGroups=new ArrayList<>();
        for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroups) {
            //检查组的id
            Integer checkgroupId = setmealCheckgroup.getCheckgroupId();
            //根据检查组id查询检查组信息以及其中的检查项
            CheckGroup checkGroup = checkgroupMapper.selectById(checkgroupId);
            //根据检查组id去检查组-检查项查询对应的检查项id
            LambdaQueryWrapper<GroupItem> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(GroupItem::getGroupid,checkgroupId);
            List<GroupItem> groupItems = checkgroup_checkitemMapper.selectList(wrapper1);
            List <CheckItem>checkItems=new ArrayList<>();
            for (GroupItem groupItem : groupItems) {


                //获得检查项id;
                Integer itemid = groupItem.getItemid();
                //根据检查项id查询检查项信息
                CheckItem checkItem = checkItemMapper.selectById(itemid);
                checkItems.add(checkItem);
                System.out.println("===================="+checkItem);
            }
            checkGroup.setCheckItems(checkItems);
            checkGroups.add(checkGroup);
        }
        setmeal.setCheckGroups(checkGroups);


        return new  Result(true,"查询成功",setmeal);
    }
}
